package top.wilsonlv.jaguar.security.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.wilsonlv.jaguar.commons.web.response.JsonResult;
import top.wilsonlv.jaguar.commons.web.response.ResultCode;

/**
 * @author lvws
 * @since 2021/4/5
 */
@Slf4j
@Configuration
@RestControllerAdvice
public class SecurityExceptionHandler {


    /**
     * 已定义的基础异常
     */
    @ResponseBody
    @ExceptionHandler(value = AccessDeniedException.class)
    public JsonResult<Void> accessDeniedException(AccessDeniedException exception) {
        log.error(exception.getMessage(), exception);
        return new JsonResult<>(ResultCode.FORBIDDEN, null, exception.getMessage());
    }

}
